/**
 * Project Name:ConsoleServiceImpl.java
 * File Name:ConsoleServiceImpl.java
 * Date:2018/8/24 下午3:32
 * Copyright (c) 2018, zhang.xiangyu@foxmail.com All Rights Reserved.
*/
package com.meatball.base.system.console.service.impl;

import com.meatball.base.system.console.service.ConsoleService;
import com.meatball.base.system.user.dao.SysPlatformPrivateKeyMapper;
import com.meatball.base.system.user.dao.SysUserMapper;
import com.meatball.base.system.user.model.SysPlatformPrivateKey;
import com.meatball.base.system.user.model.SysUser;
import com.meatball.constant.MeatballConst;
import com.meatball.utils.RSAUtils;
import com.meatball.vo.RSAKeys;
import com.meatball.vo.ResultMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Title: ConsoleServiceImpl.java
 * @Description: todo(控制台服务实现类)
 * @Author 張翔宇
 * @Date 2018/8/24 下午3:32
 * @Version V1.0
 */
@Service
public class ConsoleServiceImpl implements ConsoleService {
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysPlatformPrivateKeyMapper sysPlatformPrivateKeyMapper;

    @Override
    public ResultMsg add(SysUser record) {
        SysPlatformPrivateKey privateKey = new SysPlatformPrivateKey();

        // 获取密钥
        RSAKeys rsaKeys = RSAUtils.getKeys();
        record.setPlatformPublicKey(rsaKeys.getPublicKey());
        sysUserMapper.updateByPrimaryKeySelective(record);
        // 保存平台私钥
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(record.getId());
        if(sysUser.getPlatformPublicKey().isEmpty()) {
            privateKey.setUserId(record.getId());
            privateKey.setPlatformPrivateKey(rsaKeys.getPrivateKey());
            sysPlatformPrivateKeyMapper.insertSelective(privateKey);
        }
        return new ResultMsg(MeatballConst.ResultCode.SUCCESS.CODE, MeatballConst.ResultCode.SUCCESS.MESSAGE);
    }

    @Override
    public ResultMsg info(Long id) {
        return new ResultMsg(MeatballConst.ResultCode.SUCCESS.CODE, sysUserMapper.selectByPrimaryKey(id));
    }

    @Override
    public RSAKeys createRsa() {
        RSAKeys rsaKeys = RSAUtils.getKeys();
        return rsaKeys;
    }
}
